package com.cai310.lottery.web.controller.lottery.zc;

import java.util.Calendar;
import java.util.Date;

import javax.annotation.Resource;

import net.sf.ehcache.Cache;
import net.sf.ehcache.Element;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import com.cai310.lottery.SczcConstant;
import com.cai310.lottery.common.Lottery;
import com.cai310.lottery.common.SalesMode;
import com.cai310.lottery.dto.lottery.zc.SczcSchemeDTO;
import com.cai310.lottery.entity.lottery.zc.SczcMatch;
import com.cai310.lottery.entity.lottery.zc.SczcScheme;
import com.cai310.lottery.exception.DataException;
import com.cai310.lottery.service.lottery.SchemeEntityManager;
import com.cai310.lottery.service.lottery.SchemeService;
import com.cai310.lottery.service.lottery.zc.ZcMatchEntityManager;
import com.cai310.lottery.service.lottery.zc.impl.SczcSchemeServiceImpl;
import com.cai310.lottery.web.controller.WebDataException;
import com.cai310.lottery.web.controller.lottery.SchemeBaseController;
import com.cai310.orm.XDetachedCriteria;
import com.cai310.utils.DateUtil;
import com.cai310.utils.Struts2Utils;

@Namespace("/" + SczcConstant.KEY)
@Action(value = "scheme")
public class SczcSchemeController extends
		SchemeBaseController<SczcScheme, SczcSchemeDTO, SczcSchemeCreateForm, SczcSchemeUploadForm> {
	private static final long serialVersionUID = 5783479221989581469L;

	@Autowired
	private SczcSchemeServiceImpl schemeService;

	@Resource
	private SchemeEntityManager<SczcScheme> sczcSchemeEntityManagerImpl;

	@Resource
	private ZcMatchEntityManager<SczcMatch> sczcMatchEntityManagerImpl;

	@Autowired
	@Qualifier("sczcMatchCache")
	private Cache matchCache;
	
	private String playType;
	
	@Override
	protected SchemeService<SczcScheme, SczcSchemeDTO> getSchemeService() {
		return schemeService;
	}

	@Override
	protected SchemeEntityManager<SczcScheme> getSchemeEntityManager() {
		return sczcSchemeEntityManagerImpl;
	}

	// -------------------------------------------------------
	@Override
	protected XDetachedCriteria buildFilterListDetachedCriteria() {
		XDetachedCriteria c = super.buildFilterListDetachedCriteria();
		return c;
	}
	
	@Override
	protected String doEditNew() throws Exception {
		SczcMatch[] matchs = findMatchsOfCacheable(this.period.getId());
		if (matchs == null)
			throw new WebDataException("对阵不存在.");
		Struts2Utils.setAttribute("matchs", matchs);
		return super.doEditNew();
	}

	protected SczcMatch[] findMatchsOfCacheable(Long periodId) {
		Element el = matchCache.get(periodId);
		if (el == null) {
			SczcMatch[] matchs = sczcMatchEntityManagerImpl.findMatchs(periodId);
			if (matchs != null)
				matchCache.put(new Element(periodId, matchs));

			return matchs;
		} else {
			return (SczcMatch[]) el.getValue();
		}
	}

	protected String getKey(Date time) {
		Calendar c = Calendar.getInstance();
		c.setTime(time);
		c.add(Calendar.HOUR_OF_DAY, -10);
		String dateStr = DateUtil.dateToStr(c.getTime(), "yyyy-MM-dd");
		String weedStr = com.cai310.utils.DateUtil.getWeekStr(c);
		StringBuilder sb = new StringBuilder();
		sb.append(dateStr).append(" ").append(weedStr);
		return sb.toString();
	}

	@Override
	protected SczcSchemeDTO buildSchemeDTO() throws WebDataException {
		if (createForm == null)
			throw new WebDataException("表单数据为空.");

		if (createForm.getItems() == null || createForm.getItems().length == 0)
			throw new WebDataException("投注内容不能为空.");

		SczcSchemeDTO dto = super.buildSchemeDTO();

		return dto;
	}

	@Override
	protected String doShow() throws WebDataException {
		if (scheme != null) {
			SczcMatch[] matchs = findMatchsOfCacheable(scheme.getPeriodId());
			Struts2Utils.setAttribute("matchs", matchs);
			try {
				if(scheme.getMode() == SalesMode.COMPOUND)
				  Struts2Utils.setAttribute("items", scheme.getCompoundContent());
			} catch (DataException e) {
				e.printStackTrace();
			}
		}
		return super.doShow();
	}

	// -------------------------------------------------------

	/**
	 * @return {@link #createForm}
	 */
	public SczcSchemeCreateForm getCreateForm() {
		return createForm;
	}

	/**
	 * @param createForm the {@link #createForm} to set
	 */
	public void setCreateForm(SczcSchemeCreateForm createForm) {
		this.createForm = createForm;
	}

	/**
	 * @return {@link #uploadForm}
	 */
	public SczcSchemeUploadForm getUploadForm() {
		return uploadForm;
	}

	/**
	 * @param uploadForm the {@link #uploadForm} to set
	 */
	public void setUploadForm(SczcSchemeUploadForm uploadForm) {
		this.uploadForm = uploadForm;
	}

	@Override
	public Lottery getLotteryType() {
		return Lottery.SCZC;
	}

	public String getPlayType() {
		return playType;
	}

	public void setPlayType(String playType) {
		this.playType = playType;
	}

}
